﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;

namespace BLL
{
    public class TeacherService
    {
        private TeachersDAL teacherDAL = new TeachersDAL();
        private RolesDAL roleDAL = new RolesDAL();

        public void CreateTeacher(BoTeachers teacher)
        {
            teacherDAL.Insert(teacher);
        }

        public void UpdateTeacher(BoTeachers teacher)
        {
            teacherDAL.Update(teacher);
        }

        public void DeleteTeacher(BoTeachers teacher)
        {
            teacherDAL.Delete(teacher.TEA_ID);
        }
        public void DeleteTeacherByAcademyId(string academyId)
        {
            teacherDAL.DeleteTeacherByAcademyId(academyId);
        }

        public BoTeachers GetTeacher(string id)
        {
            return teacherDAL.GetModel(id);
        }

        public BoTeachers GetTeacherByUserName(string userName)
        {
            return teacherDAL.GetTeacherByUserName(userName);
        }

        public IList<BoTeachers> GetAllTeachers()
        {
            return teacherDAL.GetAllTeachers();
        }
        /// <summary>
        /// 得到分页的数据列表
        /// </summary>
        /// <param name="orderCon">排序条件</param>
        /// <param name="sqlCon">查询条件</param>
        /// <param name="pagesize">每页显示大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="records">返回查询到的记录</param>
        /// <returns>返回对象列表</returns>
        public IList<BoTeachers> getPageCompetitions(string sqlCon, string orderCon, int pagesize, int pageIndex, out int records)
        {
            return teacherDAL.getPageCompetitions(sqlCon, orderCon, pagesize, pageIndex, out records);
        }

        public IList<BoTeachers> GetTeachersByAcademy(string academyId)
        {
            return teacherDAL.GetTeachersByAcademy(academyId);
        }

        public IList<BoRoles> GetRolesByTeacchers(string teacherId)
        {
            return roleDAL.GetRolesByeTeacher(teacherId);
        }

        public void CreateTeacherAndRoles(BoTeachers teacher, IList<BoRoles> roles)
        {
            teacherDAL.Insert(teacher);
            TeacherVo t = new TeacherVo();
            t.TeacherId = teacher.TEA_ID;
            List<RoleVo> roleVos = new List<RoleVo>();
            foreach (BoRoles br in roles)
            {
                RoleVo r = new RoleVo();
                r.RoleId = br.ROL_ID;
                roleVos.Add(r);
            }
            t.Roles = roleVos;
            t.SaveRoles();
        }

        public void ModifyTeacherAndRoles(BoTeachers teacher, IList<BoRoles> roles)
        {
            teacherDAL.Update(teacher);
            TeacherVo t = new TeacherVo();
            t.TeacherId = teacher.TEA_ID;
            List<RoleVo> roleVos = new List<RoleVo>();
            foreach (BoRoles br in roles)
            {
                RoleVo r = new RoleVo();
                r.RoleId = br.ROL_ID;
                roleVos.Add(r);
            }
            t.Roles = roleVos;
            t.SaveRoles();
        }

        /// <summary>
        /// 通过教师编号来获取教师对象
        /// </summary>
        /// <param name="TeacherCode">教师编号</param>
        /// <returns>返回教师对象</returns>
        public BoTeachers GetTeacherByCode(string TeacherCode)
        {
            return teacherDAL.GetTeacherByCode(TeacherCode);
        }

    }
}
